<?php

namespace app\site\controller;

use app\common\model\UserModel;
use think\Controller;
use think\Request;

class PublicController extends Controller
{
    private $userModel;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->userModel = new UserModel();
    }

    /**
     * 系统用户登录
     * @return mixed
     */
    public function Login()
    {
        return $this->fetch();
    }

    /**
     * 用户登录
     */
    public function doLogin()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->param();
            $result = $this->validate($data, ['account' => 'require', 'password' => 'require']);
            if (true !== $result) {
                $this->error($result);
            }
            $res = $this->userModel->checkMemberLogin($data['account'], $data['password']);
            if ($res === false) {
                $this->error($this->userModel->getError());
            }
            $des = str_decode($res);
            if (empty($des)) {
                $this->error('未知错误！');
            }
            $des = explode(config('base.login_token'), $des);
            if (count($des) == 2) {
                if ($des[1] > 0) {
                    cookie('siteDes', $res);
                    $this->success('登录成功！', url('Index/index'));
                } else {
                    $this->error('您还未授权！');
                }
            } else {
                $this->error('非法参数！');
            }
        } else {
            $this->error('非法请求！');
        }
    }

}
